package com.henry.framework.helper;

import com.henry.framework.annotation.Action;
import com.henry.framework.bean.Handler;
import com.henry.framework.bean.Request;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by 峥桂 on 2017/5/6.
 */
public final class ControllerHelper {

    private static final Map<Request, Handler> ACTION_MAP = new HashMap<Request, Handler>();

    static {
        //
        Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();
        if (CollectionUtils.isNotEmpty(controllerClassSet)) {
            controllerClassSet.stream().forEach(
                    controllerClass -> {
                        Method[] methods = controllerClass.getDeclaredMethods();
                        if (ArrayUtils.isNotEmpty(methods)) {
                            for (Method method : methods) {
                                if (method.isAnnotationPresent(Action.class)) {
                                    Action action = method.getAnnotation(Action.class);
                                    String maapping = action.value();
                                   if(maapping.matches("\\w+:/\\w*")){
                                       String[] array=maapping.split(":");
                                       if(ArrayUtils.isNotEmpty(array) && array.length==2){
                                           String requestMethod=array[0];
                                           String requestPath=array[1];
                                           Request request=new Request(requestMethod,requestPath);
                                           Handler handler=new Handler(controllerClass, method);
                                           ACTION_MAP.put(request,handler);
                                       }
                                   }
                                }
                            }
                        }
                    }
            );
        }

    }

    public static Handler getHandler(String requestMethod,String requestPath){
        Request request=new Request(requestMethod,requestPath);
        return ACTION_MAP.get(request);

    }
}
